package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;

import com.alibaba.fastjson.JSON;
import com.tanhua.autoconfig.templates.HuanXinTemplate;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.vo.NearUserVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TanhuaService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private RecommendUserApi recommendUserApi;

    @DubboReference
    private QuestionApi questionApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private UserLocationApi userLocationApi;
    //根据 touserId  查询score
    public TodayBest todayBest() {
        //获取id
        Long toUserId = UserHolder.getUserId();
        //查询最高相似度
       RecommendUser recommendUser= recommendUserApi.findMaxScore(toUserId);
        //新用户
        if (recommendUser == null){
            recommendUser =  new RecommendUser();
            recommendUser.setToUserId(toUserId);
            recommendUser.setUserId(1L);
            recommendUser.setScore(90d);
        }
        //查询相似度最高的用户的其他信息
        UserInfo userInfo = userInfoApi.findById(recommendUser.getUserId());
        //两个 结合返回
        return TodayBest.init(userInfo,recommendUser);
    }

    public PageResult recommendation(Integer page, Integer pageSize) {
        //获取用户id
        Long toUserId = UserHolder.getUserId();
        //分页查询
       List<RecommendUser> list = recommendUserApi.findRecommendUserList(toUserId,page,pageSize);
       //如果为空 构造数据
        if (CollUtil.isEmpty(list)){
            list = defaultList();
        }
        //查询其他信息
        ArrayList<TodayBest> vos = new ArrayList<>();
        for (RecommendUser user : list) {
            //根据userid 查询其他信息
            UserInfo userInfo = userInfoApi.findById(user.getUserId());
            //信息合并
            TodayBest todayBest = TodayBest.init(userInfo, user);
            //添加集合
            vos.add(todayBest);
        }
        //返回
        PageResult result = new PageResult(page, pageSize, 0L, vos);
        return result;
    }
    //构造数据
    private List<RecommendUser> defaultList() {
        List<RecommendUser> list = new ArrayList<>();
        //构造默认推荐数据，企业中按照项目经理要求
        for (int i = 1; i <= 10; i++) {
            RecommendUser ru = new RecommendUser();
            ru.setUserId((long) i); //模拟推荐的用户id
            ru.setScore(95D); //模拟推荐的评分
            list.add(ru);
        }
        return list;
    }
    //查看佳人信息
    public TodayBest personalInfo(Long userId) {
        //查询 UserInfo  RecommendUser
        //查询用户信息 UserInfo
        UserInfo userInfo = userInfoApi.findById(userId);
        //查询推荐用户数据 缘分值 RecommendUser
        Long toUserId = UserHolder.getUserId();
        RecommendUser user = recommendUserApi.findByUserId(toUserId,userId);
       return TodayBest.init(userInfo,user);
    }
    //查询陌生人问题
    public String strangerQuestions(Long userId) {
        Question question = questionApi.findQuestion(userId);
        if (question==null){
            return "hello world";
        }
        return question.getTxt();
    }
    //回复陌生人问题
    public void reply(Long userId, String reply) {
/**
* {"userId":106,
 * "huanXinId":"hx106",
 * "nickname":"黑马小妹",
 * "strangerQuestion":"你喜欢去看蔚蓝的大海还是去爬巍峨的高山？",
 * "reply":"我喜欢秋天的落叶，夏天的泉水，冬天的雪地，只要有你一切皆可~"}
* */
        Long id = UserHolder.getUserId();
        //创建map
        Map map = new HashMap();
        map.put("userId",id);
        map.put("huanXinId","hx"+id);
        //查询用户信息
        UserInfo userInfo = userInfoApi.findById(id);
        map.put("nickname",userInfo.getNickname());
        map.put("strangerQuestion",questionApi.findQuestion(id).getTxt());
        map.put("reply",reply);
        //转换成json
        String s = JSON.toJSONString(map);
        huanXinTemplate.sendMsg("hx"+userId,s);


    }
    //搜附近
    public List<NearUserVo> search(String gender, String distance) {
        Long userId = UserHolder.getUserId();
        //搜索附近的人id
         List<Long> ids = userLocationApi.searchNear(userId,distance);
        //查询id的用户信息
        Map<Long, UserInfo> map = userInfoApi.findByIds(ids);
        //返回
        List<NearUserVo> vos = new ArrayList<>();
        for (Long id : ids) {
            //自己跳过
            if (id==userId){
                continue;
            }
            UserInfo userInfo = map.get(id);
            if (!gender.equals(userInfo.getGender())){
                continue;
            }
            //不是性别跳过
            NearUserVo vo = NearUserVo.init(userInfo);
            vos.add(vo);
        }
        return vos;
    }
}
